Page({

  /**
   * 页面的初始数据
   */
  data: {
    carts:[], 
    selectAllStatus:false,
    totalPrice:0
  },
  book:function(e){
    var that=this;
    wx.request({
      url: 'http://localhost/weixin/caizhuangshop/goumaichenggong.php',
      success:function(res){
        wx.showToast({
          title: '购买成功',
        })
        that.setData({
          carts:[]
        })
      }
    })
    
  },
    getTotalPrice:function(){
      var sum=0;
      for(var i=0;i<this.data.carts.length;i++){
        if(this.data.carts[i].selected){
          sum = sum + this.data.carts[i].price * this.data.carts[i].num;
        }
      }
      this.setData(
        {
          totalPrice:sum.toFixed(1)
        })
    },
    select:function(e){
        //console.log(e);
        var index=e.currentTarget.dataset.index;
        var carts=this.data.carts;
        carts[index].selected=!carts[index].selected;
        this.setData({
          carts:carts
        })
        this.getTotalPrice();
    },
    removweLine:function(index){
      var  carts=this.data.carts;
      carts.splice(index,1);//从数组index位置删除一条
      this.setData({
        carts:carts
      })
    },
    deleteList:function(e){
     // console.log(e);
     var index=e.currentTarget.dataset.index;
     this.removweLine(index);
     this.getTotalPrice();
    },
    minusCount:function(e){
      //console.log(e);
      var index=e.currentTarget.dataset.index;
      var carts=this.data.carts;
      if(carts[index].num>1){
        carts[index].num--;
        this.setData({carts:carts})
      }else{
        this.removweLine(index);
      }
      this.getTotalPrice();
    },
    addCount:function(e){
      //console.log(e);
      var index=e.currentTarget.dataset.index;
      var carts=this.data.carts;
      carts[index].num++;
        this.setData({carts:carts})
        this.getTotalPrice();
    },
    selectAll:function(e){
      var carts=this.data.carts;
      var seleteAllItem=!this.data.selectAllStatus;
      for(var i=0;i<carts.length;i++){
        carts[i].selected=seleteAllItem;
      }
      this.setData({
        carts:carts,
        selectAllStatus:seleteAllItem
      })
      this.getTotalPrice();
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    this.getTotalPrice();
    wx.request({
      url: 'http://localhost/weixin/caizhuangshop/gouwucheshuju.php',
      success(res){
        console.log(res);
        that.setData({
          carts:res.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})